<template>
	<a-card :bordered="false" class="mt-2">
		<a-table ref="tableRef" :columns="columns" :data-source="dataList" bordered
				 :scroll="{ x: 'max-content' }" :customRow="customRowHandler"/>
	</a-card>
</template>

<script setup>
import monthlyApi from "@/api/bi/goals/monthly/monthlyApi";
import router from "@/router";
const tableRef = ref();
const dataList = ref([]);
const columns = [
	{
		title: '项目名称',
		dataIndex: 'projectCode',
		customCell: (record, index) => {
			return {
				style: {
					display: record.rowSpan === 0 ? "none" : undefined,
				},
				rowSpan: record.rowSpan
			};
		}
	},
	{
		title: '所属月份',
		dataIndex: 'belongingMonthName',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '批次',
		dataIndex: 'batchNumber',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '人数',
		dataIndex: 'personnelCount',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '案件数量',
		dataIndex: 'caseCount',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '案件总金额',
		dataIndex: 'totalArrearsAmount',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '月目标金额',
		dataIndex: 'monthlyTargetAmount',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '月累计达成金额',
		dataIndex: 'monthlyAchievementAmount',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '结佣比例',
		dataIndex: 'commissionRatio',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '佣金',
		dataIndex: 'commission',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '垫付金额',
		dataIndex: 'advancePayment',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '去掉垫付的佣金',
		dataIndex: 'commissionExclAdv',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '佣金目标',
		dataIndex: 'commissionTarget',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '佣金达成率',
		dataIndex: 'commissionAchievementRate',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	},
	{
		title: '月还款达成率',
		dataIndex: 'monRepayAr',
		customCell: (record, index) => {
			return {
				style: {
					background: record.isTotal ? "#f0f0f0" : undefined,
				}
			};
		}
	}
];

const getList = () => {
	monthlyApi.list(2).then((res) => {
		const processedData = processData(res);
		dataList.value = processedData;
	});
};
const processData = (rawData) => {
	let mergeInfo = [];
	let currentProjectCode = null;
	let rowSpanCount = 0;
	let totals = {
		personnelCount: 0,
		caseCount: 0,
		totalArrearsAmount: 0,
		monthlyTargetAmount: 0,
		monthlyAchievementAmount: 0,
		commission: 0,
		advancePayment: 0,
		commissionExclAdv: 0,
		commissionTarget: 0
	};
	rawData.forEach((item, index) => {
		if (item.projectCode !== currentProjectCode) {
			if (rowSpanCount > 0) {
				// 设置上一个项目的最后一行的 rowSpan
				mergeInfo[mergeInfo.length - rowSpanCount].rowSpan = rowSpanCount;
				// 添加小计行
				addTotalRow(mergeInfo, currentProjectCode, {...totals}, rowSpanCount);
				// 重置 totals
				totals = {
					personnelCount: 0,
					caseCount: 0,
					totalArrearsAmount: 0,
					monthlyTargetAmount: 0,
					monthlyAchievementAmount: 0,
					commission: 0,
					advancePayment: 0,
					commissionExclAdv: 0,
					commissionTarget: 0
				};
			}
			currentProjectCode = item.projectCode;
			rowSpanCount = 1;
		} else {
			rowSpanCount++;
		}
		item.rowSpan = 0;
		totals.personnelCount += item.personnelCount;
		totals.caseCount += item.caseCount;
		totals.totalArrearsAmount += item.totalArrearsAmount;
		totals.monthlyTargetAmount += item.monthlyTargetAmount;
		totals.monthlyAchievementAmount += item.monthlyAchievementAmount;
		totals.commission += item.commission;
		totals.commissionExclAdv += item.commissionExclAdv;
		totals.commissionTarget += item.commissionTarget;
		mergeInfo.push(item);
	});

	if (rowSpanCount > 0) {
		// 设置最后一个项目的最后一行的 rowSpan
		mergeInfo[mergeInfo.length - rowSpanCount].rowSpan = rowSpanCount;
		// 添加最后一个项目的的小计行
		addTotalRow(mergeInfo, currentProjectCode, {...totals}, rowSpanCount);
	}

	return mergeInfo;
};

const addTotalRow = (data, projectCode, totals) => {
	if (data.length > 0 && data[data.length - totals.rowSpan]) {
		data[data.length - totals.rowSpan].rowSpan = totals.rowSpan + 1;
	}
	totals.isTotal = true;
	totals.belongingMonthName = '小计';
	totals.batchNumber = undefined;
	data.push(totals);
};

// 自定义行处理器，用于处理行点击等事件
const customRowHandler = (record, index) => ({
	onClick: (event) => {
		if (record.belongingMonthName !== '小计') {
			console.log('准备跳转路由...' + record.projectCode)
			router.push('/bi/chart/' + record.projectCode)
		}
	},
});

onMounted(() => {
	getList();
});
</script>
